package com.link.base.base.authorizationbrand.controller;

import com.link.base.base.authorizationbrand.model.AuthorizationBrand;
import com.link.base.base.authorizationbrand.service.AuthorizationBrandService;
import com.link.base.base.contractsinfo.model.ContractsInfo;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.KeyGenerateService;
import com.link.core.basic.service.ServiceException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liushoulong
 * @date 2018-11-07
 * @desc 授权品牌 controller
 */
@Controller("AuthorizationBrandController")
@RequestMapping("/link/authorizationBrand")
public class AuthorizationBrandController extends BasicController<AuthorizationBrand> {

    @Resource
    private KeyGenerateService keyGenerateService;

    @Resource
    private AuthorizationBrandService authorizationBrandService;

    @Override
    public BasicService<AuthorizationBrand> getBasicService() throws Exception {
        return authorizationBrandService;
    }

    /**
     * 新建或更新一条记录
     * 新增时需判断，若为第一条数据，则设置主要
     */
    @RequestMapping(value = "/otherUpsert")
    @ResponseBody
    public Map<String, Object> otherUpsert(@JsonParam AuthorizationBrand entity, HttpSession session,
                                           HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(8);
        try {
            beforUpsert(entity, request);
            checkData(entity, request);
            String key = "base";
            if (!getBasicService().isInsertFlag(entity) && !checkExistDataCache(key, session, entity)) {
                throw new ServiceException("PUBLIC-002！");
            }
            if (getBasicService().isInsertFlag(entity)) {
                Long id = keyGenerateService.keyGenerate();
                Long headId = entity.getContractId();
                AuthorizationBrand object = new AuthorizationBrand();
                object.setContractId(headId);
                List<AuthorizationBrand> objectList = authorizationBrandService.queryByExamplePage(object);
                entity.setId(id);
                getBasicService().insert(entity);
                /*如果当前头对象下无其他记录，则设置为主要数据*/
                if (objectList.size() <= 0) {
                    ContractsInfo contractsInfo = new ContractsInfo();
                    contractsInfo.setId(headId);
                }
            } else {
                getBasicService().update(entity);
            }

            AuthorizationBrand newEntry = getBasicService().queryById(entity);
            result.put("newRow", newEntry);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }


    /**
     * 删除一条记录
     * 删除时如为主要，则需要设置下一条最老数据为主要
     */
    @RequestMapping(value = "/otherDeleteById")
    @ResponseBody
    public Map<String, Object> otherDeleteById(@JsonParam AuthorizationBrand entity, HttpSession session,
                                               HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(8);
        try {
            beforDelete(entity, request);
            checkData(entity, request);
            String key = "base";
            if (!checkExistDataCache(key, session, entity)) {
                throw new ServiceException("PUBLIC-031！");
            }
            AuthorizationBrand authorizationBrand = authorizationBrandService.queryById(entity);

            getBasicService().deleteById(entity);

            /*判断当前删除数据是不是为主要数据，如为主要则设置下一条最老数据为主要*/
            String flag = "Y";
            if (flag.equals(authorizationBrand.getMainFlag())) {
                AuthorizationBrand object = new AuthorizationBrand();
                object.setContractId(authorizationBrand.getContractId());
                List<AuthorizationBrand> objectList = authorizationBrandService.queryByExamplePage(object);
                if (objectList.size() > 0) {
                    ContractsInfo contractsInfo = new ContractsInfo();
                    contractsInfo.setId(authorizationBrand.getContractId());
                }
            }
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

}
